#include "IndexBuffer.h"
#include <iostream>


int IndexBuffer::updateBuffer() {
#ifdef _OPENGL

	glGenBuffers(1, &m_BufferInfo.indexVBOID);
	glBindBuffer(GL_ARRAY_BUFFER, m_BufferInfo.indexVBOID);
	glBufferData(GL_ARRAY_BUFFER, m_IndexCount*sizeof(index_t), m_Indices, glBufferUsageFlag());

	if(glGetError() == GL_OUT_OF_MEMORY) {
		std::cerr << "Not enough device memory to create the index buffer" << std::endl;
		return 1;
	}

#endif

#ifdef _DIRECTX
	// TODO: DX IndexBuffer.updateBuffer()
	return 1;
#endif

	return 0;
}

void IndexBuffer::releaseBuffer() {
#ifdef _OPENGL
	glDeleteBuffers(1, &m_BufferInfo.indexVBOID);
#endif

#ifdef _DIRECTX
	// TODO: DX IndexBuffer.releaseBuffer()
#endif
}

void IndexBuffer::setupPipeline() {
#ifdef _OPENGL
	glEnableClientState(GL_INDEX_ARRAY);
#endif

#ifdef _DIRECTX
	// TODO: DX IndexBuffer.setupPipeline()
#endif
}

void IndexBuffer::releasePipeline() {
#ifdef _OPENGL
	glDisableClientState(GL_INDEX_ARRAY);
#endif

#ifdef _DIRECTX
	// TODO: DX IndexBuffer.releasePipeline()
#endif
}



int IndexBuffer::sendToPipeline() {

#ifdef _OPENGL
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_BufferInfo.indexVBOID);
	glDrawElements(GL_TRIANGLES, m_IndexCount, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0)); 
#endif

#ifdef _DIRECTX
	// TODO: DX IndexBuffer.sendToPipeline()
	return 1;
#endif

	return 0;

}